package cn.ityao.wall.controller;

import cn.ityao.wall.entity.TUser;
import cn.ityao.wall.service.ITOptionService;
import cn.ityao.wall.service.ITUserService;
import cn.ityao.wall.util.DataResult;
import cn.ityao.wall.util.TokenUtil;
import cn.ityao.wall.util.encryption.DesUtil;
import com.auth0.jwt.interfaces.Claim;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端登录
 * </p>
 *
 * @author tongyao
 * @since 2023-02-14
 */
@RestController
@Log4j2
public class AuthController {

    @Autowired
    private ITUserService itUserService;

    @Autowired
    private ITOptionService itOptionService;

    @RequestMapping("/login")
    public DataResult login(String username,String password, HttpServletRequest httpServletRequest){
        TUser tUser = itUserService.loadUserByUsername(username);
        String pass = DesUtil.encryption(password);
        if(tUser == null){
            throw new RuntimeException("当前用户不存在");
        }else if(!tUser.getPassWord().equals(pass)){
            throw new RuntimeException("账号或密码错误！");
        }

        int expireDate = Integer.parseInt(itOptionService.getOption("expireDate"));
        String secret = itOptionService.getOption("secret");
        // 创建token
        String accessToken = TokenUtil.createToken(tUser.getUserId(), tUser.getUserName(), expireDate, secret);

        // 解密
        Map<String, Claim> userData = TokenUtil.parsingToken(accessToken, secret);
        String parsedUserId = userData.get("userId").asString();
        String parsedUserName = userData.get("userName").asString();
        log.info("username: {}", parsedUserName);
        tUser.setAccess_token(accessToken);

        /**
         * 保存用户状态
         */
        httpServletRequest.setAttribute("access_token", accessToken);
        tUser.setLastTime(new Date());
        itUserService.updateById(tUser);
        return DataResult.setResult(tUser);
    }
}
